package com.fyt.algorithm.Leecode.day8_5;

import com.fyt.algorithm.Leecode.day4.MaxArea;

public class TrapWater {

    public int trap(int[] height) {
        int ans=0;
        int left=0,right = (height.length-1);
        int leftMax =0,rightMax = 0;

        while (left < right){
            leftMax= Math.max(height[left],leftMax);
            rightMax=Math.max(height[right],rightMax);
            if(leftMax<rightMax){
                ans+=leftMax-height[left];
                left++;
            }
            else {
                ans+=rightMax-height[right];
                right--;
            }

        }


        return ans;
    }

    public static void main(String[] args) {
        int[] height = {0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.println(new TrapWater().trap(height));
    }
}
